<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/vue.js" type="text/javascript">
			
		</script>
	</head>
	<body>
		<div id="app">
			<cn @item_increment="father_increment" @item_decrement="father_decrement" :counter="counter"></cn>
		</div>
		
		<template id="temp">
			<div>
				<h2>当前计数:{{counter}}</h2>
				<button @click="add(increment)" type="button">+</button>
				<button @click="sub(decrement)" type="button">-</button>
			</div>
		</template>
		
		<script type="text/javascript">
			const app = new Vue({
				el:"#app",
				data:{
					counter:0,
				},
				methods:{
					father_increment(item){
						console.log(item);
						this.counter++;
					},
					father_decrement(item){
						console.log(item);
						this.counter--;
					}
				},
				components:{
					cn:{
						template:"#temp",
						// data:{counter:0},错误写法
						data(){
							return {increment:"increment",decrement:"decrement"}
						},
						props:["counter"],
						methods:{
							add(item){
								this.$emit("item_increment",item);
							},
							sub(item){
								this.$emit("item_decrement",item);
							}
						}
					}
				}
			})
		</script>
	</body>
</html>
